package com.itheima.travel.controller;

import com.itheima.travel.domain.*;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.CartUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;

@RestController
@RequestMapping(value = "/cart", produces = "application/json;charset=utf-8")
public class CartController {
    @Autowired
    private RouteService routeService;
    @Autowired
    private CartUtils cartUtils;

    @RequestMapping("/addCart")
    public ResultInfo addCart(Integer num, Integer rid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        // 获取用户购物车信息
        Cart cart = cartUtils.getCartFromRedis(user);

        LinkedHashMap<Integer, CartItem> cartItemMap = cart.getCartItemMap();

        CartItem cartItem = null;
        // 添加rid商品数量
        if (cartItemMap.containsKey(rid)) {
            cartItem = cartItemMap.get(rid);
            cartItem.setNum(cartItem.getNum() + num);
        } else {
            //新增商品
            cartItem = new CartItem();
            Route route = routeService.findRouteByRid(rid);
            cartItem.setRoute(route);
            cartItem.setNum(num);
            cartItemMap.put(rid, cartItem);
        }

        // 重新计算购物车每件商品累计数
        int sum = cartItemMap.values().stream().mapToInt(CartItem::getNum).sum();
        cart.setCartNum(sum);
        //计算总额值
        double cartTotal = cartItemMap.values().stream().map(item ->
                item.getRoute().getPrice() * item.getNum()
        ).reduce(0d, (a, b) -> a + b);
        cart.setCartTotal(cartTotal);

        cartUtils.setCartToRedis(user, cart);

        return new ResultInfo(true, "购物车添加成功", cartItem);

    }

    @RequestMapping("/findAll")
    public ResultInfo findAll(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Cart cart = cartUtils.getCartFromRedis(user);
        return new ResultInfo(true, cart);
    }

    @RequestMapping("/deleteCartItem")
    public ResultInfo deleteCartItem(Integer rid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        //获取该用户购物车信息
        Cart cart = cartUtils.getCartFromRedis(user);

        // 移除rid商品
        LinkedHashMap<Integer, CartItem> cartItemMap = cart.getCartItemMap();
        cartItemMap.remove(rid);


        // 重新计算购物车每件商品累计数
        int sum = cartItemMap.values().stream().mapToInt(CartItem::getNum).sum();

        cart.setCartNum(sum);

        //计算总额值
        double cartTotal = cartItemMap.values().stream().map(item ->
                item.getRoute().getPrice() * item.getNum()
        ).reduce(0d, (a, b) -> a + b);
        cart.setCartTotal(cartTotal);

        cartUtils.setCartToRedis(user, cart);

        return new ResultInfo(true, "删除成功", cart);
    }
}
